//app.js
App({
  onLaunch: function () {
    let that = this;
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log(res)
        let {code} = res;
        that.http('/user/openid','GET',
            {
              code: code,
            },
            function (res) {
              console.log(res)
            },
            function (err) {
              
            }
            )
        
      },
      fail: err => {
        console.log(err)
      }
    })
    // 获取系统信息
    wx.getSystemInfo({
      success: res => {
        console.log(res)
        let barHeight = res.statusBarHeight;
        let gap = null;
        let width = null;
        let mnuButton = null;
        let navTop = null;
        let navHeader = null;
        let pxToRpxScale = 750 / res.windowHeight; // 比例换算
        if (res.platform === 'android') {
          gap = 8;
          width = 96;
        } else if (res.platform === 'devtools') {
          gap = res.system.search('iOS') !== -1 ? 5.5 : 7.5;
        } else {
          gap = 4;
          width = 88
        }
        console.log(wx.getMenuButtonBoundingClientRect())
        mnuButton = wx.getMenuButtonBoundingClientRect ? wx.getMenuButtonBoundingClientRect() : 
        {
          bottom: barHeight + gap + 32,
          height: 32,
          left: res.windowWidth - width - 10,
          right: res.windowWidth - 10,
          top: barHeight + gap,
          width: width
        };
        navTop = mnuButton.top;

        console.log(mnuButton)
        console.log(barHeight)
        
        navHeader = barHeight + mnuButton.height + (navTop - barHeight) * 2;

        that.globalData.screenWidth = res.screenWidth;
        that.globalData.navHeader = navHeader;
        that.globalData.navWidth = mnuButton.width;
        that.globalData.navHeight = mnuButton.height + (navTop - barHeight) * 2;
        that.globalData.barHeight = barHeight;
        that.globalData.navTop = navTop - barHeight;
        that.globalData.navTitle = mnuButton.left - mnuButton.width - 20;
        that.globalData.windowHeight = res.windowHeight - that.globalData.navHeader;
      },
      fail: err => {
        console.log(err)
      },
      complete: (res) => {},
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              //console.log(res)
              // 可以将 res 发送给后台解码出 unionId
              that.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (that.userInfoReadyCallback) {
                that.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  //请求 
  http: function (url,method,data,success,error) {
    let that = this;
    wx.request({
      url: that.globalData.baseURL + '/wx' + url,
      data: data,
      timeout: 5000,
      header: method === 'get' ? {
        'content-type': 'application/xml'
      } : {
        'Content-Type':'application/x-www-form-urlencoded'
      },
      success: res => {
        success(res);
      },
      fail: err => {
        console.log(err);
        error(err)
      }
    })
    
  },
  globalData: {
    userInfo: null,
    screenWidth: null,
    navWidth: null, // 胶囊按钮宽度
    navHeader: null, // 头部高度 通知栏  标题栏
    navHeight: null, // 标题栏高度
    barHeight: null, // 通知栏高度
    navTop: null, // 通知栏到胶囊按钮的边距
    navTitle: null, // 标题 title 宽度
    windowWidth: null, // 高度
    baseURL: 'http://192.168.11.116:3000',
  }

})